<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2019/3/9
 * Time: 16:55
 */

namespace app\admin\controller;

use app\admin\model\ProxyMoney;
use app\admin\model\Redpack as MchModel;
use app\admin\validate\Redpack as MchValidate;
use think\Db;
use think\db\Where;
use Naixiaoxin\ThinkWechat\Facade;


class Redpack extends Base {
    protected static $payment  = null;
    public function __construct()
    {
        parent::__construct();
        $this->model = new MchModel;
        $this->validate = new MchValidate;
        self::$payment = Facade::payment();
    }

    /**
     * @param int $limit
     * @param int $page
     * @return \think\Response|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index($limit=10,$page=1){
        $where = new Where();
        if (self::$mc_id) {
            $where['mc_id']=self::$mc_id;
        }
        $model_name = $this->model->getTable();
        $data = request()->post();
        if($data){
            if($data['name']){
                $where[$model_name.'.name']=['like','%'.trim($data['name']).'%'];
            }
            if($data['status']){
                $where[$model_name.'.status']=$data['status'];
            }
            if($data['date']){
                $t = explode('~',$data['date']);
                $start = strtotime($t[0]);
                $end = strtotime($t[1]);
                $where[$model_name.'.create_time']= ['between time',[$start,$end]];
            }
        }
        $list = $this->model->getList($where,$limit);
        if(request()->isAjax()){
            return $this->__s('',$list);
        }else{
            $this->assign('total',$list->total());
            $this->assign('list',$list);
            return view();
        }
    }

    public function add(){
        $mch = (new \app\admin\model\Mch())->get(self::$mc_id);
        $this->assign('info',$mch);
        return view();
    }

    /**
     *
     * @return \think\Response
     */
    public function insert(){
        $data = request()->post();
        $name="商户【{$data['name']}】".$data['total'].'元，红包充值';
        $result = $this->model->addNew($name,$data['proxy_id'],$data['mc_id'],
            $data['total'],$data['free'],$data['totals'],$data['type'],$out);
        if(!$result){
            return $this->__e($out);
        }
        $result->update_time = 0;
        if(!$result->isUpdate(false)->save($out)){
            return $this->__e('申请失败');
        }
        return $this->__s('申请成功',['redirect'=>Url('redpack/index')]);
    }

    /**
     * @param int $id
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function check($id=0){
        if(!$id){
            $this->error('缺少参数');
        }
        $info = $this->model->find($id);
        $this->assign('info',$info);
        return view();
    }

    /**
     * 确认代理
     * @param int $id
     * @param int $status
     * @param string $info
     * @return \think\Response
     * @throws \think\exception\PDOException
     */
    public function update($id=0,$status=0,$info=''){
        if(!$id){
            return $this->__e('缺少参数');
        }
        $info = $this->model->updateStatus($id,$status,$info,self::$setting,$out);
        if(!$info){
            return $this->__e($out);
        }
        return $this->__s('操作成功',['redirect'=>Url('redpack/index')]);
    }

    public function status($id=0,$status=0){
        if(!$id || !$status){
            $this->__e('缺少参数');
        }
        $mch = $this->model->get($id);
        if(!$mch){
            $this->__e('商户不存在');
        }
        if($status==2){
            $mch->cross_time = time();
        }
        $mch->status = $status;
        if(!$mch->isUpdate(true)->save()){
            $this->__e('操作失败');
        }
        $this->__s('操作成功');
    }


    /**
     * 删除
     * @param int $id
     * @throws \Exception
     */
    public function del($id=0){
        if(!$id){
            $this->__e('缺少参数');
        }
        if(!$this->model->where('id','eq',$id)->delete()){
            $this->__e('删除失败');
        }
        $this->__s('删除成功');
    }
}